package java复习202408.G_IO验证;


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.junit.jupiter.api.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class g1_filetest {

    /**
     * 字节和大小的关系
     */
    @Test
    public void test2(){
        String a = "a你";
        char c1 = 'a';
        System.out.println("c1 = " + c1);
        System.out.println((int)c1);

        char c2 = '你';
        System.out.println("c2 = " + c2);
        System.out.println((int)c2);

        byte[] bytes = a.getBytes(StandardCharsets.UTF_8);
        System.out.println(bytes.length);
    }


    @Test
    public void test1() throws IOException {
        File file = new File("D:/filetest.txt");
        System.out.println("插入数据之前的file.length = " + file.length());

        byte[] bytes = new byte[(int) file.length()];
        FileInputStream fileInputStream = new FileInputStream(file);
        fileInputStream.read(bytes);
        System.out.println(new String(bytes));

        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        fileOutputStream.write(" hello java io 1".getBytes(StandardCharsets.UTF_8));

        System.out.println("插入数据之后的file.length = " + file.length());

        byte[] bytes2 = new byte[(int) file.length()];
        FileInputStream fileInputStream2 = new FileInputStream(file);
        int read = fileInputStream2.read(bytes2);
        System.out.println("插入的数据： " + new String(bytes2));


        byte[] bytes3 = new byte[10];
        FileInputStream fileInputStream3 = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream3);
        bufferedInputStream.read(bytes3,0,10);

        System.out.println(new String(bytes3));


    }

}
